from django.db import models


class GoodsCategory(models.Model):
    """商品类别"""
    name = models.CharField(max_length=10, verbose_name='名称')
    parent = models.ForeignKey('self', related_name='subs', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父类别')
    icon = models.ImageField(max_length=200, null=True, blank=True, verbose_name='默认图片')

    class Meta:
        db_table = 'tb_goods_category'
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name


class GoodsAreaCode(models.Model):
    """省市区编码"""

    code = models.CharField(verbose_name='编码', max_length=10, help_text='编码')
    name = models.CharField(max_length=20, verbose_name='名称')
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True, verbose_name='上级行政区划')
    level = models.IntegerField(verbose_name='层级', null=True, blank=True)
    prov_name = models.CharField(verbose_name='省', max_length=64, null=True, blank=True)
    city_name = models.CharField(verbose_name='市', max_length=64, null=True, blank=True)
    district_name = models.CharField(verbose_name='区县', max_length=64, null=True, blank=True)
    prov = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='省', related_name="prov_subs")
    city = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='市', related_name="city_subs")
    district = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='区县', related_name="district_subs")
    prov_code = models.CharField(verbose_name='省', max_length=64, null=True, blank=True)
    city_code = models.CharField(verbose_name='市', max_length=64, null=True, blank=True)
    district_code = models.CharField(verbose_name='区县', max_length=64, null=True, blank=True)

    class Meta:
        db_table = 'tb_goods_area_code'
        verbose_name = '省市区编码'
        verbose_name_plural = verbose_name


class Goods(models.Model):
    """商品"""

    IS_LAUNCHED_CHOICES = (
        (0, '禁止售卖'),
        (1, '正常售卖'),
        (2, '审核中'),
        (3, '已驳回'),
        (4, '待审核')
    )

    ORDER_TYPE_CHOICES = (
        (1, '线下核销'),
        (2, '消费金'),
        (3, '兑换券'),
        (4, '线下核销(不推送)'),
        (5, '快递配送'),
        (6, '线下核销/快递配送')
    )

    IS_BUYMORE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_SETTLEMENT_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_USE_POINT_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_GROUP_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    GROUP_LIMIT_TYPE_CHOICES = (
        (1, '天'),
        (2, '时')
    )

    ORDER_SPECIES_CHOICES = (
        (1, '邮乐享'),
        (2, '微邮惠商圈'),
        (3, '小邮美宿')
    )

    name = models.CharField(max_length=256, verbose_name='名称')
    caption = models.CharField(max_length=100, verbose_name='副标题', null=True, blank=True)
    category = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, verbose_name='类别')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价', default=0)
    cost_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='进价', default=0)
    market_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='市场价', default=0)
    point = models.DecimalField(verbose_name='邮米', max_digits=10, decimal_places=0, default=0)
    default_image = models.ImageField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')
    sales = models.IntegerField(default=0, verbose_name='销量')
    stock = models.IntegerField(default=0, verbose_name='库存')
    remain_stock = models.IntegerField(default=0, verbose_name='总库存')
    comments = models.IntegerField(default=0, verbose_name='评价数')
    desc_detail = models.TextField(default='', verbose_name='详细介绍')
    desc_pack = models.TextField(default='', verbose_name='商品说明')
    desc_service = models.TextField(default='', verbose_name='售后服务')
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='供货商', on_delete=models.CASCADE)
    equityID = models.CharField(verbose_name='权益id', max_length=20, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='添加时间', null=True, blank=True)
    is_launched = models.SmallIntegerField(verbose_name='是否上架销售', choices=IS_LAUNCHED_CHOICES, default=1, blank=True)
    exchange_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='兑换金额', default=0)
    order_type = models.SmallIntegerField(verbose_name='商品下单类型', choices=ORDER_TYPE_CHOICES, default=1, blank=True)
    is_buymore = models.SmallIntegerField(verbose_name='是否允许下多件产品', choices=IS_BUYMORE_CHOICES, default=1, blank=True)
    is_settlement = models.SmallIntegerField(verbose_name='是否结算', choices=IS_SETTLEMENT_CHOICES, default=0, blank=True)
    limit_by_user_count = models.IntegerField(verbose_name='每个人最多购买数量', null=True, blank=True)
    manufacturer = models.CharField(verbose_name='厂商', max_length=64, null=True, blank=True)
    specifications = models.CharField(verbose_name='规格', max_length=64, null=True, blank=True)
    is_use_point = models.SmallIntegerField(verbose_name='是否支持邮米', choices=IS_USE_POINT_CHOICES, default=0, blank=True)
    market_url = models.CharField(verbose_name='销售码', null=True, blank=True, max_length=128)
    market_code = models.ImageField(verbose_name='销售码', null=True, blank=True)
    goods_tag = models.CharField(verbose_name='优惠标识', null=True, blank=True, max_length=32)
    sort = models.IntegerField(verbose_name='排序', null=True, blank=True)
    is_group = models.SmallIntegerField(verbose_name='是否可以拼团', choices=IS_GROUP_CHOICES, default=0)
    group_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='拼团价', default=0)
    group_order_num = models.IntegerField(verbose_name='成团数', default=1)
    group_limit_time = models.IntegerField(verbose_name='拼团时间', default=1)
    group_limit_type = models.SmallIntegerField(verbose_name='拼团时间类型', choices=GROUP_LIMIT_TYPE_CHOICES, default=1)
    longitude = models.CharField(verbose_name='经度', max_length=32, null=True, blank=True)
    latitude = models.CharField(verbose_name='纬度', max_length=32, null=True, blank=True)
    area = models.ForeignKey('goods.GoodsAreaCode', verbose_name='省市区', on_delete=models.SET_NULL, null=True, blank=True)
    order_species = models.SmallIntegerField(verbose_name='商品下单类型', choices=ORDER_SPECIES_CHOICES, default=1)
    proof = models.ImageField(verbose_name='商品凭证', null=True, blank=True)

    class Meta:
        db_table = 'tb_goods'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsAuditRecord(models.Model):
    '''商品审核记录'''

    AUDIT_TYPE_CHOICES = (
        (1, '新增'),
        (2, '审核'),
    )

    goods = models.ForeignKey('goods.Goods', verbose_name='商品', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    create_user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.CASCADE)
    audit_type = models.SmallIntegerField(verbose_name='操作类型', choices=AUDIT_TYPE_CHOICES, default=1)
    remark = models.CharField(verbose_name='备注', max_length=128)

    class Meta:
        db_table = 'tb_goods_audit_record'
        verbose_name = '商品审核记录'
        verbose_name_plural = verbose_name


class GoodsCoupons(models.Model):

    STATUS_CHOICES = (
        (0, '未激活'),
        (1, '待核销'),
        (2, '已核销'),
        (3, '已过期'),
        (4, '已撤销')
    )

    coupons_id = models.CharField(max_length=32, verbose_name='券id', unique=True)
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    order = models.ForeignKey('orders.OrderInfo', on_delete=models.SET_NULL, verbose_name='订单', null=True, blank=True)
    status = models.SmallIntegerField(verbose_name='券状态', choices=STATUS_CHOICES, default=0)
    coupons_qrcode = models.ImageField(verbose_name='普码', null=True, blank=True)
    validity_time = models.DateTimeField(verbose_name='有效期', null=True, blank=True)
    use_time = models.DateTimeField(verbose_name='核销时间', null=True, blank=True)
    use_user = models.ForeignKey('users.User', verbose_name='核销人', on_delete=models.SET_NULL, null=True, blank=True)
    use_merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='核销人', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_goods_coupons'
        verbose_name = '商品券'
        verbose_name_plural = verbose_name


class LaunchedGoods(models.Model):
    """上架商品"""

    IS_LAUNCHED_CHOICES = (
        (0, '已下架'),
        (1, '已上架'),
        (2, '审核中'),
        (3, '已驳回')
    )

    IS_DEFAULT_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_SELF_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    name = models.CharField(max_length=256, verbose_name='名称')
    caption = models.CharField(max_length=100, verbose_name='副标题', null=True, blank=True)
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    category = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, verbose_name='从属类别')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价', default=0)
    point = models.DecimalField(verbose_name='邮米', max_digits=10, decimal_places=0, default=0)
    stock = models.IntegerField(default=0, verbose_name='库存')
    sales = models.IntegerField(default=0, verbose_name='销量')
    is_launched = models.SmallIntegerField(verbose_name='是否上架销售', choices=IS_LAUNCHED_CHOICES, default=1, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='销售网点', on_delete=models.SET_NULL, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='上架时间', null=True, blank=True)
    is_default = models.SmallIntegerField(verbose_name='默认唯一商品', choices=IS_DEFAULT_CHOICES, default=1, blank=True)
    market_url = models.CharField(verbose_name='销售码', null=True, blank=True, max_length=128)
    market_code = models.ImageField(verbose_name='销售码', null=True, blank=True)
    is_self = models.SmallIntegerField(verbose_name='默认唯一商品', choices=IS_SELF_CHOICES, default=0, blank=True)

    class Meta:
        db_table = 'tb_launched_goods'
        verbose_name = '上架商品'
        verbose_name_plural = verbose_name


class GoodsImage(models.Model):
    """Goods图片"""

    IS_DEFAULT_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    is_default = models.SmallIntegerField(verbose_name='默认唯一商品', choices=IS_DEFAULT_CHOICES, default=1, blank=True)
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='Goods')
    image = models.ImageField(verbose_name='图片')
    applyment = models.ForeignKey('goods.GoodsModifyApplyment', on_delete=models.SET_NULL, verbose_name='GoodsModifyApplyment', null=True, blank=True)

    class Meta:
        db_table = 'tb_goods_image'
        verbose_name = 'Goods图片'
        verbose_name_plural = verbose_name


class GoodsVisitCount(models.Model):
    """商品访问量"""

    category = models.ForeignKey(LaunchedGoods, on_delete=models.CASCADE, verbose_name='商品分类')
    count = models.IntegerField(verbose_name='访问量', default=0)
    date = models.DateField(auto_now_add=True, verbose_name='统计日期')

    class Meta:
        db_table = 'tb_goods_visit'
        verbose_name = '商品访问量'
        verbose_name_plural = verbose_name


class GoodsModifyApplyment(models.Model):
    '''商品修改申请单'''

    APPLYMENT_TYPE_CHOICES = (
        (1, '修改商品'),
        (2, '新增商品')
    )

    APPLYMENT_STATUS_CHOICES = (
        (1, '审核中'),
        (2, '已通过'),
        (3, '已驳回')
    )

    IS_LAUNCHED_CHOICES = (
        (0, '已下架'),
        (1, '已上架')
    )

    goods = models.ForeignKey(Goods, verbose_name='商品', on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField(max_length=256, verbose_name='名称', null=True, blank=True)
    applyment_type = models.SmallIntegerField(verbose_name='操作类型', choices=APPLYMENT_TYPE_CHOICES, default=1, blank=True)
    applyment_status = models.SmallIntegerField(verbose_name='申请单状态', choices=APPLYMENT_STATUS_CHOICES, default=1, blank=True)
    caption = models.CharField(max_length=100, verbose_name='副标题', null=True, blank=True)
    desc_detail = models.TextField(verbose_name='详细介绍', null=True, blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价', null=True, blank=True)
    market_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='市场价', null=True, blank=True)
    default_image = models.ImageField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')
    image_array = models.TextField(verbose_name='详细介绍', null=True, blank=True)
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='所属商户', on_delete=models.SET_NULL, null=True, blank=True)
    stock = models.IntegerField(default=0, verbose_name='库存')
    companyuser = models.ForeignKey('users.User', verbose_name='派发客户经理', on_delete=models.SET_NULL, null=True, blank=True)
    manufacturer = models.CharField(verbose_name='厂商', max_length=64, null=True, blank=True)
    specifications = models.CharField(verbose_name='规格', max_length=16, null=True, blank=True)
    is_launched = models.SmallIntegerField(verbose_name='是否上架销售', choices=IS_LAUNCHED_CHOICES, default=1, blank=True)
    barcode = models.CharField(verbose_name='厂商', max_length=20, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='操作时间', null=True, blank=True)
    reject_reason = models.CharField(verbose_name='驳回原因', max_length=1024, null=True, blank=True)
    update_word = models.TextField(verbose_name='更新字段', null=True, blank=True)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    goods_tag = models.CharField(verbose_name='优惠标识', null=True, blank=True, max_length=32)
    desc_pack = models.TextField(verbose_name='商品说明', null=True, blank=True)

    class Meta:
        db_table = 'tb_goods_modify_applyment'
        verbose_name = '商品修改申请单'
        verbose_name_plural = verbose_name


class GoodsTagActivity(models.Model):
    '''优惠标识活动'''

    ACTIVITY_STATE_CHOICES = (
        (1, '进行中'),
        (2, '已停止')
    )

    goods_tag = models.CharField(verbose_name='优惠标识', max_length=32)
    activity_name = models.CharField(verbose_name='活动名称', max_length=64)
    activity_state = models.SmallIntegerField(verbose_name='状态', choices=ACTIVITY_STATE_CHOICES, default=1)
    create_user = models.ForeignKey('users.User', verbose_name='添加人', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)
    level = models.ForeignKey('users.Level', verbose_name='地市', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_goods_tag_activity'
        verbose_name = '优惠标识活动'
        verbose_name_plural = verbose_name


class GoodsTagActivityLevelRelation(models.Model):
    '''优惠标识活动'''

    goods_tag_activity = models.ForeignKey('goods.GoodsTagActivity', verbose_name='优惠标识', on_delete=models.CASCADE)
    level = models.ForeignKey('users.Level', verbose_name='地市', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_goods_tag_activity_level_relation'
        verbose_name = '优惠标识活动'
        verbose_name_plural = verbose_name
